/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.domain.das;

import cn.hallele.infrastructure.metadata.constant.MsgCodeConst;
import cn.hallele.infrastructure.metadata.exception.children.ActionException;
import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.usercenter.domain.domain.DeptDomain;
import cn.hallele.ms.usercenter.model.cache.DeptCache;
import cn.hallele.ms.usercenter.model.entity.DeptEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/11/12 23:14
 */
@Slf4j
@Component
public class DeptDEDas extends ModelDas<DeptCache, DeptDomain, DeptEntity> {
    protected DeptDEDas(DeptCache option) {
        super(option);
    }

    /**
     * 分页查询所有部门名
     *
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @param condition 查询条件
     * @return 分页结果
     */
    public Page<DeptDomain> findAll(int pageNum, int pageSize, DeptDomain condition) {
        return revertPage(this.option.findAllByCondition(convert(condition), pageNum, pageSize));
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptDomain findOne(DeptDomain condition) {
        return revert(this.option.findOne(convert(condition)));
    }

    /**
     * 新增 部门
     *
     * @param deptDomain 部门
     */
    public void addOne(DeptDomain deptDomain) {
        if (this.option.exists(convert(deptDomain))) {
            log.error("Dept {} already exists.Add failed.", deptDomain.getName());
            throw new ActionException(MsgCodeConst.DATABASE_DATA_EXIST, deptDomain.getName());
        }
        this.option.addOne(convert(deptDomain));
    }

    /**
     * 更新 部门
     *
     * @param deptDomain 部门
     */
    public void updateOne(DeptDomain deptDomain) {
        if (!this.option.existsById(deptDomain.getId())) {
            log.error("Dept {} not exists. Update failed", deptDomain.getName());
            throw new ActionException(MsgCodeConst.DATABASE_DATA_NOT_EXIST, deptDomain.getName());
        }
        this.option.update(convert(deptDomain));
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void delete(Integer id) {
        if (!this.option.existsById(id)) {
            log.error("Dept {} not exists.Delete failed", id);
            throw new ActionException(MsgCodeConst.DATABASE_DATA_NOT_EXIST, id);
        }
        this.option.deleteOneById(id);
    }

    /**
     * 更新 部门 状态
     *
     * @param id         主键
     * @param deptStatus 状态
     */
    public void updateDeptStatus(Integer id, DeptStatusEnum deptStatus) {
        DeptEntity oneById = this.option.findOneById(id);
        if (oneById == null) {
            log.error("Dept {} not exists.Update failed", id);
            throw new ActionException(MsgCodeConst.DATABASE_DATA_NOT_EXIST, id);
        }
        int i = this.option.updateStatus(id, oneById.getStatus(), deptStatus);

        if (i == 1) {
            return;
        }

        log.error("Data {} can not update, maybe already changed.Now status: {}.Target status: {}.Number Of impacts: {}",
                oneById, oneById.getStatus(), deptStatus, i);
        throw new ActionException(MsgCodeConst.DATABASE_DATA_NOT_EXIST, id);

    }

    /**
     * 将T转成E
     *
     * @param deptDomain 被转换者
     * @return S 转换后者
     */
    @Override
    protected DeptEntity convert(DeptDomain deptDomain) {
        return ObjectUtil.map(deptDomain, d -> DeptEntity.builder()
                .id(d.getId())
                .number(d.getNumber())
                .name(d.getName())
                .description(d.getDescription())
                .leader(d.getLeader())
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param deptEntity 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DeptDomain revert(DeptEntity deptEntity) {
        return ObjectUtil.map(deptEntity, d -> DeptDomain.builder()
                .id(d.getId())
                .number(d.getNumber())
                .name(d.getName())
                .description(d.getDescription())
                .leader(d.getLeader())
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }
}
